#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>



void reserve(char* p1)
{
    int len = strlen(p1);
    int i = 0;
    int tmp = 0;
    while (i < len / 2)
    {
        tmp = p1[i];
        p1[i] = p1[len - 1 - i];
        p1[len - 1 - i] = tmp;
        i++;
    }
}

char* addBinary(char* a, char* b) {
    int lena = strlen(a);
    int lenb = strlen(b);
    reserve(a);
    reserve(b);
    int nlen = lena > lenb ? lena : lenb;
    int carry = 0;
    int len = 0;
    char* ans = (char*)malloc(sizeof(char) * (nlen + 2));
    for (int i = 0; i < nlen; i++)
    {
        carry += i < lena ? (a[i] == '1') : 0;
        carry += i < lenb ? (b[i] == '1') : 0;
        ans[len++] = carry % 2 + '0';
        carry /= 2;
    }
    if (carry)
    {
        ans[len++] = '1';
    }
    ans[len] = '\0';
    reserve(ans);
    return ans;
}


int main()
{
    char arr1[] = { "101011" };
    char arr2[] = { "1011"};
    addBinary(arr1, arr2);
    return 0;
}